package com.cmc.cloud.cmclink.doc.rpcservice.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.ApiErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.rpcservice.CntrTransferService;
import com.cmc.cloud.cmclink.eqm.api.model.request.CommonCntrTypeQueryParam;
import com.cmc.cloud.cmclink.eqm.api.model.response.CommonCntrTypeDTO;
import com.cmc.cloud.cmclink.eqm.api.resource.CommonCntrTypeApi;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 箱型基础信息
 * 
 * @author dev89
 * @date 2025/08/05 13:37
 **/
@Service
@Validated
@Slf4j
public class CntrTransferServiceImpl implements CntrTransferService {
    @Resource
    CommonCntrTypeApi cntrApi;

    private static final String GET_CNTR_TYPE_EXCEPTION = "查询CNTR第三方接口失败";

    private static final String GET_CNTR_TYPE_NULL = "查询CNTR第三方接口返回空值";

    @Override
    public List<String> getCntrTypeStringListExist(List<String> sizeTypeList) {
        CommonCntrTypeQueryParam param = new CommonCntrTypeQueryParam();
        // e.getCntrSize() + e.getCntrType()
        param.setSizeTypeList(sizeTypeList);
        List<String> resultList = new ArrayList<>();
        List<CommonCntrTypeDTO> result = getCntrTypeList(param);
        if (CollectionUtil.isNotEmpty(result)) {
            resultList = result.stream().map(e -> e.getCntrSize() + e.getCntrType()).collect(Collectors.toList());
        }
        return resultList;
    }

    @Override
    public List<CommonCntrTypeDTO> getCntrTypeList(CommonCntrTypeQueryParam param) {
        CommonResult<List<CommonCntrTypeDTO>> commonResult;
        try {
            commonResult = cntrApi.query(param);
        } catch (Exception e) {
            log.error(GET_CNTR_TYPE_EXCEPTION, e);
            throw ServiceExceptionUtil.exception(ApiErrorCodeConstants.CNTR_GET_DATA_FAILED);
        }

        if (commonResult == null) {
            log.error(GET_CNTR_TYPE_NULL);
            throw ServiceExceptionUtil.exception(ApiErrorCodeConstants.CNTR_GET_DATA_NULL);
        }

        return commonResult.getCheckedData();
    }
}
